package com.UI;

import com.Model.CurricularDbAdapter;
import com.Model.StudentDBModel;
import android.app.ListActivity;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

/*creating Extra Curricular activities for the user*/

public class ExtraCurricular extends ListActivity {
	
    private static final int ACTIVITY_EDIT= 1;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private CurricularDbAdapter DbHelper;

		
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.add_curricular);
    	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.curricular_titlebar);
		
    	((TextView)findViewById(R.id.titleCurricular)).setText("Extra Curricular Activities" );
    	
    	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			ExtraCurricular.this.finish();

    		}
    	});
   	
    	findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    		//	NotepadAddDelete.this.finish();
       	    			
    			Intent addcurricular = new Intent();
    			addcurricular.setClassName("com.UI", "com.UI.ExtraCurricularForm");
    			startActivity(addcurricular);
    			
    		}
    	});
    	   	
    	
	//	setContentView(R.layout.add_curricular);
        DbHelper = new CurricularDbAdapter(this);
        DbHelper.open();
        fillData();
        registerForContextMenu(getListView());
              
    }

    // this function will display a list of Activities that are saved
    private void fillData() {
        Cursor activityCursor = DbHelper.fetchAllActivities();
        startManagingCursor(activityCursor);
        
        // Create an array to specify the fields we want to display in the list (only NAME)
        String[] from = new String[]{CurricularDbAdapter.KEY_NAME,CurricularDbAdapter.KEY_DAY, CurricularDbAdapter.KEY_TIME };
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1,R.id.text2,R.id.text3};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter activity = new SimpleCursorAdapter(this, R.layout.add_curricular_row, activityCursor, from, to);
        setListAdapter(activity);
    }
	
//    // when user taps Add Activities button, go to Edit/View Activities
//    public void addActivity(View component) {
//
//		Intent addActivities = new Intent(this, ExtraCurricularForm.class);
//		startActivity(addActivities);
//	}
   
    // create an in-built menu item 
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.delete_activity);
	}

    // when in-built menu item (this case delete) is selected we delete note
    @Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()) {
    	case DELETE_ID:
    		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        DbHelper.deleteActivity(info.id);
	        fillData();
	        return true;
		}
		return super.onContextItemSelected(item);
	}

    // this chooses the right row_id of the activity that is selected
    // and activates the onActivityResult method
    // @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ExtraCurricularForm.class);
        i.putExtra(CurricularDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    // this activity is called when startActivityForResult is called
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }

	
}		

	

